package com.cqjtu.education.controller;

import com.cqjtu.education.entity.Course;
import com.cqjtu.education.service.CourseService;
import com.cqjtu.education.utils.AliyunOSS;
import com.cqjtu.education.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Course)表控制层
 *
 * @author makejava
 * @since 2022-04-13 14:11:02
 */
@Api(tags = "课程相关接口")
@RestController
@RequestMapping("course")
@Slf4j
public class CourseController {
    /**
     * 服务对象
     */
    @Resource
    private CourseService courseService;

    @Resource
    private AliyunOSS aliyunOSS;

    @RequestMapping("/test")
    public Result test(@RequestPart("file") MultipartFile file) {
        try {
            return Result.success(aliyunOSS.uploadPublic(file));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("操作失败");
    }

    @ApiOperation(value = "uploadCourseImage", notes = "上传课程封面")
    @RequestMapping("/uploadCourseImage")
    public Result uploadCourseImage(String cid, @RequestPart("file") MultipartFile file) {
        if (file == null) {
            return Result.fail("文件为空");
        }
        try {
            return Result.success(courseService.uploadCourseImage(cid, file));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("操作失败");
    }

    /**
     * 创建课程，将老师和课程关联起来
     *
     * @param tids
     * @param cid
     * @return
     */
    @RequestMapping("/link")
    @ApiOperation(value = "link", notes = "将老师与授课课程关联起来（多个教师）")
    public Result link(@RequestParam("tids") List<String> tids, @RequestParam("cid") String cid) {
        try {
            return Result.success(courseService.link(tids, cid));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("操作失败");
    }

    @RequestMapping("/linked")
    @ApiOperation(value = "linked", notes = "将老师与授课课程关联起来（单个教师）")
    public Result linked(@RequestParam("tid") String tid, @RequestParam("cid") String cid) {
        try {
            return Result.success(courseService.linked(tid, cid));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("操作失败");
    }

    @RequestMapping("/queryByTeacherId")
    @ApiOperation(value = "queryByTeacherId", notes = "通过教师id查询开授课程")
    public Result queryByTeacherId(@RequestParam("teacherId") String teacherId) {
        try {
            return Result.success(courseService.queryByTeacherId(teacherId));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("查询失败");
    }

    /**
     * 根据课程开放时间查询
     */
    @RequestMapping("/queryByOpenTime")
    @ApiOperation(value = "queryByOpenTime", notes = "根据课程开放时间查询课程")
    public Result queryByOpenTime(String pageNum, String pageSize) {
        List<Course> list = new ArrayList<>();
        try {
            list = courseService.queryByOpenTime(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("发生异常");
        }
        return Result.success(list);
    }

    /**
     * 根据价格查询
     */
    @RequestMapping("/queryByPrice")
    @ApiOperation(value = "queryByPrice", notes = "根据课程价格查询")
    public Result queryByPrice(String low, String high, String pageNum, String pageSize) {
        List<Course> list=new ArrayList<>();
        try {
            list = courseService.queryByPrice(Double.parseDouble(low), Double.parseDouble(high), Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("发生异常");
        }
        return Result.success(list);
    }

    /**
     * 查询所有
     */
    @RequestMapping("/getAllCourses")
    @ApiOperation(value = "getAllCourses", notes = "查询所有课程")
    public Result getAllCourses(String pageNum, String pageSize) {
        return Result.success(courseService.getAllCourses(Integer.parseInt(pageNum), Integer.parseInt(pageSize)));
    }
    /**
     * 根据id查询单个
     */
    @RequestMapping("/queryById")
    @ApiOperation(value = "queryById", notes = "根据id查询单个课程")
    public Result queryById(String id) {
        return Result.success(courseService.queryById(id));
    }

    /**
     * 有课程封面时新增课程
     *
     * @param course
     * @param multipartFile
     * @return
     */

    @RequestMapping("/customAdd")
    @ApiOperation(value = "customAdd", notes = "有课程封面时新增课程，需要教师id")
    public Result customAdd(@RequestParam String tid, @RequestPart("course") Course course, @RequestPart("file") MultipartFile multipartFile) {
        try {
            course.setCover(aliyunOSS.uploadPublic(multipartFile));
            courseService.insert(course);
            courseService.linked(tid, course.getId());
        } catch (Exception e) {
            log.info("add（自定义封面）");
            log.error(e.toString());
            return Result.fail("新增课程失败");
        }
        return Result.success(course);
    }

    /**
     * 模糊查询
     *
     * @param key 关键字
     * @return 单条数据
     */
    @RequestMapping("/fuzzyQuery")
    @ApiOperation(value = "fuzzyQuery", notes = "模糊查询")
    public Result fuzzyQuery(String key, String pageNum, String pageSize) {
        List<Course> list=new ArrayList<>();
        try {
            list = courseService.fuzzyQuery(key, Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("发生异常");
        }
        return Result.success(list);
    }

    @RequestMapping("/countByKey")
    @ApiOperation(value = "countByKey", notes = "通过关键字查询总条数")
    public Result countByKey(String key) {
        int total = 0;
        try {
            total = courseService.countByKey(key);
            Map<String, Integer> map = new HashMap<>();
            map.put("total", total);
            return Result.success(map);
        } catch (Exception e) {
            log.info("countByKey");
            log.error(e.toString());
        }
        return Result.fail("查询失败");
    }

    /**
     * 默认课程封面新增课程
     *
     * @param course 实体
     * @return 新增结果
     */
    @RequestMapping("/defaultAdd")
    @ApiOperation(value = "defaultAdd", notes = "默认课程封面新增课程，需要教师id")
    public Result defaultAdd(@RequestBody Course course, @RequestParam String tid) {
        try {
            course.setCover("https://cqjtu-education.oss-cn-beijing.aliyuncs.com/course_cover/default_cover.png");
            courseService.insert(course);
            courseService.linked(tid, course.getId());
        } catch (Exception e) {
            log.info("add（默认封面）");
            log.error(e.toString());
            return Result.fail("新增课程失败");
        }
        return Result.success(course);
    }

    /**
     * 编辑数据
     *
     * @param course 实体
     * @return 编辑结果
     */
    @RequestMapping("/edit")
    @ApiOperation(value = "edit", notes = "更新课程")
    public Result edit(@RequestBody Course course) {
        try {
            courseService.update(course);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("发生异常");
        }
        return Result.success(course);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping("/deleteById")
    @ApiOperation(value = "deleteById", notes = "通过id删除课程")
    public Result deleteById(String id) {
        try {
            courseService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("发生异常");
        }
        return Result.success("删除成功");
    }

}

